//
//  AdMoreNativeAdController.m
//  AdMoreSDKDemo
//
//  Created by Hayder on 2023/12/7.
//

#import "AdMoreNativeAdController.h"

@interface AdMoreNativeAdController ()<AdMoreNativeAdManagerDelegate,AdMoreNativeAdDelegate>

@property (nonatomic, strong) AdMoreNativeAd *nativeAd;

@property (nonatomic, strong) AdMoreNativeAdManager *adManager;

@property (nonatomic, strong) UIView *lastShowView;

@end

@implementation AdMoreNativeAdController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupBtn];
    
    //adManager可以重复使用
    self.adManager = [[AdMoreNativeAdManager alloc] initWithSlotID:kNativeID rootController:[UIApplication sharedApplication].keyWindow.rootViewController adSize:CGSizeMake(HH_SCREEN_WIDTH, 0)];
    self.adManager.delegate = self;
}

- (void)setupBtn
{
    self.margin = (HH_SCREEN_WIDTH - self.btnW)/2;
    self.loadDataBtn.frame = CGRectMake(self.margin, self.y, self.btnW, self.btnH);
    
    self.showDataBtn.hidden = YES;
    self.closeBtn.hidden = YES;
}

- (void)loadEvent
{
    [self.view showActivityHUD];
    //示例只演示1个的情况
    [self.adManager loadAdDataWithCount:1];
}

#pragma mark ---------------------AdMoreNativeAdManagerDelegate----------------------------
/**信息流广告加载成功*/
- (void)nativeAdViewsLoadSuccess:(NSArray <AdMoreNativeAd *>*)nativeAds
{
    [self.view hideActivityHUD];
    if(nativeAds.count > 0)
    {
        self.nativeAd = nativeAds.firstObject;
        self.nativeAd.delegate = self;
    }
}
/**信息流广告加载失败**/
- (void)nativeAdViewsFailedToLoadWithError:(NSError *)error
{
    [self.view hideActivityHUD];
}

#pragma mark ---------------------AdMoreNativeAdDelegate----------------------------
/**信息流渲染成功---多次回调*/
- (void)nativeAdViewRenderSuccess:(UIView *)nativeAdView nativeAd:(nonnull AdMoreNativeAd *)nativeAd
{
    [self.lastShowView removeFromSuperview];
    //修改信息流广告的位置
    //渲染成功后添加到要展示的view上
    nativeAdView.y = self.showDataBtn.bottom + 10;
    [self.view addSubview:nativeAdView];
    self.lastShowView = nativeAdView;
    
    [self.view hideActivityHUD];
}
/**信息流渲染失败*/
- (void)nativeAdViewFailedToRender:(UIView *)nativeAdView nativeAd:(nonnull AdMoreNativeAd *)nativeAd error:(nonnull NSError *)error
{
    [self.view hideActivityHUD];
}

- (void)nativeAdViewWillExpose:(UIView *)nativeAdView nativeAd:(AdMoreNativeAd *)nativeAd
{
    NSLog(@"nativeAd:信息流展示成功");
}

/**信息流点击事件**/
- (void)nativeAdViewDidClick:(UIView *)nativeAdView nativeAd:(nonnull AdMoreNativeAd *)nativeAd
{
    NSLog(@"nativeAd:信息流点击");
}
/**信息流关闭事件*/
- (void)nativeAdViewDidClose:(UIView *)nativeAdView nativeAd:(nonnull AdMoreNativeAd *)nativeAd
{
    NSLog(@"nativeAd:信息流关闭");
    [nativeAdView removeFromSuperview];
}

/**视频停止播放**/
- (void)nativeAdViewPlayerStopPlaying:(UIView *)nativeAdView nativeAd:(nonnull AdMoreNativeAd *)nativeAd
{
    NSLog(@"nativeAd:视频完成播放");
}
/**视频播放**/
- (void)nativeAdViewVideoStartPlaying:(UIView *)nativeAdView nativeAd:(nonnull AdMoreNativeAd *)nativeAd
{
    NSLog(@"nativeAd:视频播放");

}
/**视频暂停播放**/
- (void)nativeAdViewVideoDidPause:(UIView *)nativeAdView nativeAd:(nonnull AdMoreNativeAd *)nativeAd
{
    NSLog(@"nativeAd:视频暂停");
}


@end
